//
//  WriteViewController.m
//  PipeNote
//
//  Created by lanou3g on 15/12/18.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "WriteViewController.h"
#import "DiaryViewController.h"
#import "AppDelegate.h"
#import "Write.h"
#import "PhotoWrite.h"
@interface WriteViewController ()

{
    UIImageView *imageView;
    UITextView *textView;
    UIBarButtonItem *rightItem;
}
//用于手势识别
@property (nonatomic,assign) BOOL isTap;

@end

@implementation WriteViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建一个formatter对象
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //设置时间格式
    [formatter setDateFormat:@" yyyy-MM-dd "];
    self.title = [NSString stringWithFormat:@"%@",[formatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:0]]];
    
    //背景图片
    [self background];
  
    
    if (ScreenFrame_Widdth == 320){
        
        textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 70, ScreenFrame_Widdth, 150)];
    }
    if (ScreenFrame_Widdth == 375){
        
        textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 130, ScreenFrame_Widdth, 280)];
    }
    if (ScreenFrame_Widdth == 414){
        
        textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 150, ScreenFrame_Widdth, 300)];
    }
    
    textView.backgroundColor = [UIColor clearColor];
    //设置字体大小
    textView.font = [UIFont boldSystemFontOfSize:18];
    //日记开始时间
    textView.text = [NSString stringWithFormat:@"           %@          ",[formatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:0]]];
    //键盘自动弹出
    [textView becomeFirstResponder];
    [self.view addSubview:textView];
    
    //返回首页
    [self homePage];
    
    
    //保存数据
    [self saveData];
    

    //手势
    [self gesture];
   
}

//返回首页
- (void)homePage{
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAction:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
}
//返回首页事件
- (void)leftItemAction:(UIBarButtonItem *)sender{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

//保存数据
- (void)saveData{
    
    //通知保存按钮什么时候可以按
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(testing) name:UITextViewTextDidChangeNotification object:nil];

    
    rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"保存.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightItemAction:)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    rightItem.enabled = NO;
}

//通知保存按钮什么时候可以按
- (void)testing{
    
    if (textView.text.length >= 1) {
        
        rightItem.enabled = YES;
        
    }else{
        
        rightItem.enabled = NO;
    }
    
    
}


//保存数据事件
- (void)rightItemAction:(UIBarButtonItem *)semder{
    
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    Write *write = [NSEntityDescription insertNewObjectForEntityForName:@"Write" inManagedObjectContext:app.managedObjectContext];
    write.content = textView.text;
    //保存数据
    [app saveContext];
    
    
    [self dismissViewControllerAnimated:YES completion:nil];

}

//手势
- (void)gesture{
    //轻拍手势
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGRAction)];
    //设置点击次数
    tapGR.numberOfTapsRequired = 2;
    //设置手指个数
    tapGR.numberOfTouchesRequired = 2;
    //添加手势
    [self.view addGestureRecognizer:tapGR];
    
    //让isTap初值为NO
    self.isTap = NO;
}
//手势事件
- (void)tapGRAction{
    
    if (self.isTap == NO) {
        self.isTap = YES;
        textView.hidden = YES;
    }else{
        self.isTap = NO;
        textView.hidden = NO;
    }
    
}



//点空白处回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}


- (void)background{
    
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
    imageView.frame = CGRectMake(0, 64, ScreenFrame_Widdth, ScreenFrame_Height);
    [self.view addSubview:imageView];
    
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    
    //创建搜索请求对象，并且指明所有的数据类型
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"PhotoWrite"];
    NSArray *objcArray = [app.managedObjectContext executeFetchRequest:request error:nil];
    
    PhotoWrite *photo = objcArray.lastObject;
    
    UIImage *image = [UIImage imageWithData:photo.photo];
    imageView.image = image;
    
    if (objcArray.count == 0) {
        
        imageView.image = [UIImage imageNamed:@"写日记.jpg"];
    }

    
}








@end
